﻿using System.Net;
using System.Net.Mail;

namespace Helper
{
    public class MailHelper
    {
        public string fromMail { get; set; }
        public string toMail { get; set; }
        public string fromName { get; set; }
        public string toMailName { get; set; }
        public string fromPassword { get; set; }
        public string subject { get; set; }
        public string body { get; set; }
        public string host { get; set; }
        public int port { get; set; }
        public bool enableSsl { get; set; }
        public bool useDefaultCredential { get; set; }
        public string attachment { get; set; }

        public MailHelper()
        {
            fromMail = string.Empty;
            toMail = string.Empty;
            fromName = string.Empty;
            toMailName = string.Empty;
            fromPassword = string.Empty;
            subject = string.Empty;
            body = string.Empty;
            host = "smtp.gmail.com";
            port = 587;
            enableSsl = true;
            useDefaultCredential = false;
            attachment = string.Empty;
        }

        public void Send()
        {

            var fromAddress = new MailAddress(fromMail, fromName);
            var toAddress = new MailAddress(toMail, toMailName);


            var smtp = new SmtpClient
                       {
                           Host = host,
                           Port = port,
                           EnableSsl = enableSsl,
                           DeliveryMethod = SmtpDeliveryMethod.Network,
                           UseDefaultCredentials = useDefaultCredential,
                           Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                       };


            using (var message = new MailMessage(fromAddress, toAddress)
                                 {
                                     Subject = subject,
                                     Body = body
                                 })
            
            {
                if (attachment != string.Empty)
                {
                    var att = new Attachment(attachment);
                    message.Attachments.Add(att);
                }
                smtp.Send(message);
            }
        }
    }
}
